package pATT.bNEdit.gui;

import java.awt.AWTEvent;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.util.Enumeration;

import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDesktopPane;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.JToolBar;
import javax.swing.event.MenuEvent;

import pATT.bNEdit.base.RedBayes;
import pATT.bNEdit.learning.AlgoritmoAprendizajeEstructural;


/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2004</p>
 * <p>Company: </p>
 * @author Martin Blech && Luciana Zavaleta
 * @version 1.0
 */

public class BNEditMainFrame extends JFrame{
  /**
	 * 
	 */
	private static final long serialVersionUID = 1L;
public final int TABLE_PROB =0;
  public final int TABLE_EXP = 1;

  private int numNew = 0;
  private int numWindows = 0;
  private final static int newOffset = 10;
  private String currDir = null;
  private int table=TABLE_PROB;

  JDesktopPane desktopPane = new JDesktopPane();
  JPanel contentPane;

  JMenuBar jMenuPrincBar = new JMenuBar();
  JToolBar toolBar = new JToolBar();

  JLabel status = new JLabel();

  JMenu jMenuFile = new JMenu();
  JMenu jMenuHelp = new JMenu();
  JMenu jMenuNetwork = new JMenu();
  JMenu jMenuTables = new JMenu();

  ButtonGroup buttonGroup1 = new ButtonGroup();

  JButton buttonNew = new JButton();
  JButton buttonOpen = new JButton();
  JButton buttonSave = new JButton();
  JButton buttonSaveAll = new JButton();
  JButton buttonSL = new JButton();

  JRadioButtonMenuItem jBtMenuProbTable = new JRadioButtonMenuItem();
  JRadioButtonMenuItem jBtMenuExpTable = new JRadioButtonMenuItem();

  JMenuItem jMenuFileExit = new JMenuItem();
  JMenuItem jMenuHelpAbout = new JMenuItem();
  JMenuItem jMenuFileNew = new JMenuItem();
  JMenuItem jMenuFileOpen = new JMenuItem();
  JMenuItem jMenuFileSave = new JMenuItem();
  JMenuItem jMenuFileSaveAll = new JMenuItem();
  JMenuItem jMenuSaveAs = new JMenuItem();
  JMenuItem jMenuInitialize = new JMenuItem();
  JMenuItem jMenuSumNormal = new JMenuItem();
  JMenuItem jMenuMaxNormal = new JMenuItem();
  JMenuItem jMenuSumRetracted = new JMenuItem();
  JMenuItem jMenuMaxRetracted = new JMenuItem();
  JMenuItem jMenuItemEdit = new JMenuItem();
  JMenuItem jMenuRun = new JMenuItem();
  JMenuItem jMenuNetProperties = new JMenuItem();
  JMenuItem jMenuAdaptation = new JMenuItem();
  JMenuItem jMenuAddExpTableAll = new JMenuItem();
  JMenuItem jMenuFileClose = new JMenuItem();
  JMenuItem jMenuFileCloseAll = new JMenuItem();

  //Construct the frame
  public BNEditMainFrame() {
    enableEvents(AWTEvent.WINDOW_EVENT_MASK);
    try {
      jbInit();
    }
    catch(Exception e) {
      e.printStackTrace();
    }
  }

  //Component initialization
  private void jbInit() throws Exception  {
    contentPane = (JPanel) this.getContentPane();
    contentPane.setLayout(new BorderLayout());
    this.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
    this.setSize(new Dimension(700, 500));
    this.setTitle("BNEdit");
    this.setJMenuBar(jMenuPrincBar);
    this.setIconImage(((ImageIcon)ResourceManager.getIcon("fileIcon.gif")).getImage());


    jMenuFile.setText("File");

    jMenuFileExit.setText("Exit");
    jMenuFileExit.addActionListener(new BNEditMainFrame_jMenuFileExit_ActionAdapter(this));

    jMenuHelp.setText("Help");

    jMenuHelpAbout.setText("About");
    jMenuHelpAbout.addActionListener(new BNEditMainFrame_jMenuHelpAbout_ActionAdapter(this));

    buttonNew.setToolTipText("Create new network");
    buttonNew.setIcon(ResourceManager.getIcon("new.gif"));
    buttonNew.addActionListener(new BNEditMainFrame_buttonNew_actionAdapter(this));

    buttonOpen.addActionListener(new BNEditMainFrame_buttonOpen_actionAdapter(this));
    buttonOpen.setIcon(ResourceManager.getIcon("open.gif"));
    buttonOpen.setToolTipText("Open XMLBIF 0.3 network file");

    buttonSave.setEnabled(false);
    buttonSave.setIcon(ResourceManager.getIcon("save.gif"));
    buttonSave.setToolTipText("Save the selected network");
    buttonSave.addActionListener(new BNEditMainFrame_buttonSave_actionAdapter(this));

    buttonSaveAll.setEnabled(false);
    buttonSaveAll.setIcon(ResourceManager.getIcon("saveAll.gif"));
    buttonSaveAll.setToolTipText("Save all modified networks");
    buttonSaveAll.addActionListener(new BNEditMainFrame_buttonSaveAll_actionAdapter(this));

    jMenuFileNew.setText("New");
    jMenuFileNew.setIcon(ResourceManager.getIcon("new.gif"));
    jMenuFileNew.addActionListener(new BNEditMainFrame_jMenuFileNew_actionAdapter(this));

    jMenuFileOpen.setText("Open");
    jMenuFileOpen.setIcon(ResourceManager.getIcon("open.gif"));
    jMenuFileOpen.addActionListener(new BNEditMainFrame_jMenuFileOpen_actionAdapter(this));

    jMenuFileSave.setEnabled(false);
    jMenuFileSave.setText("Save");
    jMenuFileSave.setIcon(ResourceManager.getIcon("save.gif"));
    jMenuFileSave.addActionListener(new BNEditMainFrame_jMenuFileSave_actionAdapter(this));

    jMenuFileSaveAll.setEnabled(false);
    jMenuFileSaveAll.setText("Save all");
    jMenuFileSaveAll.setIcon(ResourceManager.getIcon("saveAll.gif"));
    jMenuFileSaveAll.addActionListener(new BNEditMainFrame_jMenuFileSaveAll_actionAdapter(this));

    jMenuSaveAs.setEnabled(false);
    jMenuSaveAs.setToolTipText("Save the net as..");
    jMenuSaveAs.setText("Save as...");
    jMenuSaveAs.setIcon(ResourceManager.getIcon("save.gif"));
    jMenuSaveAs.addActionListener(new BNEditMainFrame_jMenuSaveAs_actionAdapter(this));

    buttonSL.setToolTipText("Load cases from file, and perform structural learning");
    buttonSL.setIcon(ResourceManager.getIcon("structlearning.gif"));
    buttonSL.addActionListener(new BNEditMainFrame_buttonSL_actionAdapter(this));

    jMenuTables.setText("Table");

    jBtMenuProbTable.setText("Show Probability Tables");
    jBtMenuProbTable.addActionListener(new BNEditMainFrame_jBtMenuProbTable_actionAdapter(this));
    jBtMenuExpTable.setText("Show Experience Tables");
    jBtMenuExpTable.addActionListener(new BNEditMainFrame_jBtMenuExpTable_actionAdapter(this));

    jMenuNetwork.setText("Network");
    jMenuNetwork.addMenuListener(new BNEditMainFrame_jMenuNetwork_menuAdapter(this));

    jMenuInitialize.setText("Initialize");
    jMenuInitialize.addActionListener(new BNEditMainFrame_jMenuInitialize_actionAdapter(this));
    jMenuInitialize.setIcon(ResourceManager.getIcon("initialize.gif"));

    jMenuSumNormal.setText("Prop Sum Normal");
    jMenuSumNormal.addActionListener(new BNEditMainFrame_jMenuSumNormal_actionAdapter(this));
    jMenuSumNormal.setIcon(ResourceManager.getIcon("propsumnormal.gif"));

    jMenuMaxNormal.setText("Prop Max Normal");
    jMenuMaxNormal.addActionListener(new BNEditMainFrame_jMenuMaxNormal_actionAdapter(this));
    jMenuMaxNormal.setIcon(ResourceManager.getIcon("propmaxnormal.gif"));

    jMenuSumRetracted.setText("Prop Sum Fast Retract");
    jMenuSumRetracted.addActionListener(new BNEditMainFrame_jMenuSumRetracted_actionAdapter(this));
    jMenuSumRetracted.setIcon(ResourceManager.getIcon("propsumfastretract.gif"));

    jMenuMaxRetracted.setText("Prop Max Fast Retract");
    jMenuMaxRetracted.addActionListener(new BNEditMainFrame_jMenuMaxRetracted_actionAdapter(this));
    jMenuMaxRetracted.setIcon(ResourceManager.getIcon("propmaxfastretract.gif"));

    jMenuItemEdit.setText("Edit");
    jMenuItemEdit.addActionListener(new BNEditMainFrame_jMenuItemEdit_actionAdapter(this));
    jMenuItemEdit.setIcon(ResourceManager.getIcon("edit.gif"));

    jMenuRun.setText("Run");
    jMenuRun.addActionListener(new BNEditMainFrame_jMenuRun_actionAdapter(this));
    jMenuRun.setIcon(ResourceManager.getIcon("run.gif"));

    jMenuNetProperties.setText("Network Properties");
    jMenuNetProperties.addActionListener(new BNEditMainFrame_jMenuNetProperties_actionAdapter(this));
    jMenuNetProperties.setIcon(ResourceManager.getIcon("netprop.gif"));

    jMenuAdaptation.setText("Adaptation");
    jMenuAdaptation.addActionListener(new BNEditMainFrame_jMenuAdaptation_actionAdapter(this));
    jMenuAdaptation.setIcon(ResourceManager.getIcon("adaptation.gif"));

    jMenuAddExpTableAll.setText("Add Experience Table to All Nodes");
    jMenuAddExpTableAll.addActionListener(new BNEditMainFrame_jMenuAddExpTableAll_actionAdapter(this));

    jMenuFileClose.setText("Close");
    jMenuFileClose.addActionListener(new BNEditMainFrame_jMenuFileClose_actionAdapter(this));

    jMenuFileCloseAll.setText("Close All");
    jMenuFileCloseAll.addActionListener(new BNEditMainFrame_jMenuFileCloseAll_actionAdapter(this));

    jMenuFile.add(jMenuFileNew);
    jMenuFile.add(jMenuFileOpen);
    jMenuFile.addSeparator();
    jMenuFile.add(jMenuFileSave);
    jMenuFile.add(jMenuSaveAs);
    jMenuFile.add(jMenuFileSaveAll);
    jMenuFile.addSeparator();
    jMenuFile.add(jMenuFileClose);
    jMenuFile.add(jMenuFileCloseAll);
    jMenuFile.addSeparator();
    jMenuFile.add(jMenuFileExit);

    jMenuHelp.add(jMenuHelpAbout);

    jMenuPrincBar.add(jMenuFile);
    jMenuPrincBar.add(jMenuNetwork);
    jMenuPrincBar.add(jMenuTables);
    jMenuPrincBar.add(jMenuHelp);

    contentPane.add(toolBar, BorderLayout.NORTH);

    toolBar.add(buttonNew, null);
    toolBar.add(buttonOpen, null);
    toolBar.add(buttonSave, null);
    toolBar.add(buttonSaveAll, null);
    toolBar.add(buttonSL, null);

    contentPane.add(status,  BorderLayout.SOUTH);
    contentPane.add(desktopPane, BorderLayout.CENTER);

    desktopPane.setDragMode(JDesktopPane.OUTLINE_DRAG_MODE);

    jMenuTables.add(jBtMenuProbTable);
    jMenuTables.add(jBtMenuExpTable);

    jMenuNetwork.add(jMenuInitialize);
    jMenuNetwork.add(jMenuSumNormal);
    jMenuNetwork.add(jMenuMaxNormal);
    jMenuNetwork.add(jMenuSumRetracted);
    jMenuNetwork.add(jMenuMaxRetracted);
    jMenuNetwork.addSeparator();
    jMenuNetwork.add(jMenuItemEdit);
    jMenuNetwork.add(jMenuRun);
    jMenuNetwork.addSeparator();
    jMenuNetwork.add(jMenuNetProperties);
    jMenuNetwork.addSeparator();
    jMenuNetwork.add(jMenuAdaptation);
    jMenuNetwork.addSeparator();
    jMenuNetwork.add(jMenuAddExpTableAll);
    jMenuNetwork.setEnabled(false);
    jMenuNetwork.setEnabled(false);

    jBtMenuExpTable.setSelected(false);
    jBtMenuProbTable.setSelected(true);

    jMenuTables.setEnabled(false);

    status.setBorder(BorderFactory.createLoweredBevelBorder());
    status.setText(" ");

  }

  /**
   * Sets to Experience table or Potencial table
   * @param table int
   */
  public void setTable(int table){
    this.table=table;
  }


  /**
   * File | New
   * Creates a new Bayesian Network and shows a BNEditInternalFrame for it
   */
  public void newBayesianNetwork() {
    String newNetName = "unnamed" + ++numNew;
    setStatus("Creating " + newNetName + "...");
    RedBayes bayesianNetwork = new RedBayes(newNetName);
    BNEditInternalFrame newFrame =
        new BNEditInternalFrame(bayesianNetwork, null, this);
    newFrame.setLocation(new Point(numWindows*newOffset, numWindows*newOffset));
    desktopPane.add(newFrame);
    newFrame.show();
    numWindows++;
    setStatus(newNetName + " created.");
  }

  /**
   * File | Open
   * Prompts the user with a File Open dialog, loads the Bayesian Network and
   * shows a BNEditInternalFrame for it.
   */
  public void openBayesianNetwork() {
    JFileChooser chooser;
    if (currDir==null)
      chooser = new JFileChooser();
    else
      chooser = new JFileChooser(currDir);
    chooser.addChoosableFileFilter(new NetFilter());
    chooser.addChoosableFileFilter(new BifFilter());
    int returnVal = chooser.showOpenDialog(this);
    if (returnVal == JFileChooser.APPROVE_OPTION) {
      currDir = chooser.getCurrentDirectory().getPath();
      String fileName = chooser.getSelectedFile().getName();
      setStatus("Loading " + fileName + "...");
      RedBayes bn = new RedBayes("");
      String filePath = chooser.getSelectedFile().getPath();
      String ext =
          filePath.substring(filePath.lastIndexOf('.')+1).toLowerCase();
      System.out.println(ext);
      
      bn.loadFromXml03File("C:/PATT Files/SAVER/xml/bayesNetXML.xml");
//      if (ext.equals("xml03"))
//          bn.loadFromXml03File(filePath);
//      
//      else if (ext.equals("net"))
//          bn.loadFromNetFile(filePath);
//      else {
//          filePath += ".xml03";
//          fileName += ".xml03";
//       }
     
      BNEditInternalFrame bayesFrame = new BNEditInternalFrame(bn, "C:/PATT Files/SAVER/xml/bayesNetXML.xml", this);
//      BNEditInternalFrame bayesFrame = new BNEditInternalFrame(bn,
//          filePath,
//          this);
      desktopPane.add(bayesFrame);
      bayesFrame.setLocation(new Point(numWindows*newOffset,
                                       numWindows*newOffset));
      bayesFrame.show();
      setStatus(fileName + " (" + bn.getNombre() + ") loaded.");
    }

  }

  /**
   * Shows the specified status in the status bar
   * @param status String - the status to show
   */
  public void setStatus(String status) {
    if ((status==null) || status.equals(""))
      this.status.setText(" ");
    else
      this.status.setText(status);
  }

  /**
   * File | Save
   * Saves the active internal frame's bayesian network
   */
  public void saveCurrent() {
    ((BNEditInternalFrame)desktopPane.getSelectedFrame()).save();
  }

  /**
   * File | Save as...
   * Saves the selected internal frame's bayesian network to a new filename
   */
  public void saveCurrentAs() {
  ((BNEditInternalFrame)desktopPane.getSelectedFrame()).saveAs();
  }

  /**
   * File | Save All
   * Saves all open bayesian networks, until the users cancels the operation
   */
  public void saveAll() {
    JInternalFrame[] frames = desktopPane.getAllFrames();
    for (int i = 0; i < frames.length; i++) {
      BNEditInternalFrame frame = (BNEditInternalFrame)frames[i];
      if (!frame.save())
        break;
    }
  }


  /**
   * File | Close
   * Closes the current network
   */
  public void closeCurrentNet() {
   ((BNEditInternalFrame) desktopPane.getSelectedFrame()).close();
  }

  /**
   * File | Close All
   * Closes all bayesian networks
   */
  public void closeAllNetworks(){
    JInternalFrame[] internalFrames = desktopPane.getAllFrames();
     for (int i = 0; i < internalFrames.length; i++) {
       BNEditInternalFrame frame = (BNEditInternalFrame) internalFrames[i];
       if (!frame.close())
         return;
     }
  }


  /**
   * File | Exit action performed
   * Main frame should close only if the user agrees to close all internal
   * frames.
  */
  public void exit() {
    closeAllNetworks();
//    System.exit(0);
  }

  //Overridden so we can exit when window is closed
  protected void processWindowEvent(WindowEvent e) {
    super.processWindowEvent(e);
    if (e.getID() == WindowEvent.WINDOW_CLOSING) {
      exit();
    }
  }

  /**
   * Network | Initialize
   * Initiates the bayesian network
   */
  public void initialize() {
    ((BNEditInternalFrame) desktopPane.getSelectedFrame()).reinicializar();
  }

  /**
   * Network | Prop Sum Normal
   * Shows the node's Probabilities: It updates all probabilities
   * according to entered evidence.
   */
  public void sumNormal() {
    ((BNEditInternalFrame) desktopPane.getSelectedFrame()).buttonPropSumNormal.setSelected(true);
    ((BNEditInternalFrame) desktopPane.getSelectedFrame()).propagate("obj17.ViewSumNormal","Sum Propagate: P(All)=");
  }

  /**
   * Network | Prop Max Normal
   * Finds states belonging to the most probable configuration
   * of the list of all nodes in the network.
   */
  public void maxNormal() {
    ((BNEditInternalFrame) desktopPane.getSelectedFrame()).buttonPropMaxNormal.setSelected(true);
    ((BNEditInternalFrame) desktopPane.getSelectedFrame()).propagate("obj17.ViewMaxNormal","Sum Max Propagate: P(All)=");
  }

  /**
   * Network | Prop Sum Fast Retract
   * Instead of showing the true posterior probability distribution for the
   * node, the distribution is shown as if the node had not received evidence.
   */
  public void sumRetracted() {
    ((BNEditInternalFrame) desktopPane.getSelectedFrame()).buttonPropSumRetract.setSelected(true);
    ((BNEditInternalFrame) desktopPane.getSelectedFrame()).propagate("obj17.ViewSumRetracted","Sum Fast Retracted Propagate: P(All)=");
  }

  /** Network | Prop Max Fast Retract
   * It is similar to the max normal method except that it shows the nodes
   * which have received evidence differently:the values of the states
   * are shown as if this node had not received evidence.
   */
  public void maxRetracted() {
    ((BNEditInternalFrame) desktopPane.getSelectedFrame()).buttonPropMaxRetract.setSelected(true);
    ((BNEditInternalFrame) desktopPane.getSelectedFrame()).propagate ("obj17.ViewMaxRetracted","Sum Max Fast Retracted Propagate: P(All)=");
  }

  /**
   * Network | Edit
   * Change to Editor type
   */
  public void edit() {
    ((BNEditInternalFrame) desktopPane.getSelectedFrame()).buttonRun.setSelected(false);
    ((BNEditInternalFrame) desktopPane.getSelectedFrame()).itemEdit_Selected();

  }

  /**
   * Network | Run
   * Change to Run type
   */
  public void run() {
    ((BNEditInternalFrame) desktopPane.getSelectedFrame()).buttonRun.setSelected(true);
    ((BNEditInternalFrame) desktopPane.getSelectedFrame()).itemRun_Selected();

  }

  /**
   * Network | Network Properties
   * Show the table of the current node. It can be an Experience table or
   * a Potencial table.
   */
  public void netProperties() {
    ((BNEditInternalFrame) desktopPane.getSelectedFrame()).showBNPropertiesDialog();

  }

  /** Network | Adaptation
   * Refines the (conditional) probabilities specified for a Bayesian network
   * by taking into consideration real experiment outcomes or cases.
   */
    public void adaptation() {
   ((BNEditInternalFrame) desktopPane.getSelectedFrame()).adaptation();
  }

  /**
   * Network | Add Experience Table to All Nodes
   * Add experience tables to all networks's nodes
   */
  public void addExpTableAll() {
    for(Enumeration variable=((BNEditInternalFrame) desktopPane.getSelectedFrame()).getBayesianNetwork().ListaVariables().elements(); variable.hasMoreElements();){
       ((BNEditInternalFrame) desktopPane.getSelectedFrame()).createExperienceTable((String) variable.nextElement());
    }
    this.setStatus("All variables added an Experience Table");
  }

  /**
   *  Table | Show Probability Tables
   *  Properties tables are shown when Node Properties is selected
   */
  void potencialTable() {
    jBtMenuExpTable.setSelected(false);
    jBtMenuProbTable.setSelected(true);
    setTable(TABLE_PROB);
    ((BNEditInternalFrame) desktopPane.getSelectedFrame()).setShowTable();

  }

  /**
   * Table | Show Experience Tables
   * Experience tables are shown when Node Properties is selected
   */
  void experienceTable() {
    jBtMenuExpTable.setSelected(true);
    jBtMenuProbTable.setSelected(false);
    setTable(TABLE_EXP);
    ((BNEditInternalFrame) desktopPane.getSelectedFrame()).setShowTable();
  }

  public int getTable(){
    return table;
  }

  //Help | About action performed
  public void helpAbout() {
    BNEditMainFrame_AboutBox dlg = new BNEditMainFrame_AboutBox(this);
    Dimension dlgSize = dlg.getPreferredSize();
    Dimension frmSize = getSize();
    Point loc = getLocation();
    dlg.setLocation((frmSize.width - dlgSize.width) / 2 + loc.x,
                    (frmSize.height - dlgSize.height) / 2 + loc.y);
    dlg.setModal(true);
    dlg.pack();
    dlg.show();
  }

  /**
   * Sets the menu options to a new enabled status
   * @param enabled boolean - the new state
   */

  private void setOptionEnabled(boolean enabled){
      jMenuInitialize.setEnabled(enabled);
      jMenuSumNormal.setEnabled(enabled);
      jMenuMaxNormal.setEnabled(enabled);
      jMenuSumRetracted.setEnabled(enabled);
      jMenuMaxRetracted.setEnabled(enabled);
      jMenuAdaptation.setEnabled(enabled);
      jMenuItemEdit.setEnabled(enabled);

  }

  void jMenuNetwork_menuSelected() {
    if (((BNEditInternalFrame) desktopPane.getSelectedFrame()).isEditorType()){
        setOptionEnabled(false);
      }
      else{
        setOptionEnabled(true);
        BNEditInternalFrame actual = (BNEditInternalFrame) desktopPane.
            getSelectedFrame();
        if (actual.getBayesianNetwork() != null) {
          String var = (String) actual.runPanel.jVar.getSelectedItem();

          if (var!=null && !actual.getBayesianNetwork().getNodo(var).hasExperienceTable())
            this.jMenuAdaptation.setEnabled(false);

        }
        else
          this.jMenuRun.setEnabled(false);
      }
  }

  void structuralLearning() {
      StructuralLearningDialog slDialog = new StructuralLearningDialog(this, true);
      slDialog.show();
      if (slDialog.getChoice() == StructuralLearningDialog.CHOICE_OK) {
        String fileName = slDialog.getFileName();
        AlgoritmoAprendizajeEstructural algorithm = slDialog.getAlgorithm();
        RedBayes bayesianNetwork = algorithm.aprenderEstructura(fileName);
        BNEditInternalFrame newFrame =
            new BNEditInternalFrame(bayesianNetwork, null, this);
        newFrame.setLocation(new Point(numWindows*newOffset, numWindows*newOffset));
        desktopPane.add(newFrame);
        newFrame.show();
        numWindows++;
      }
    }

  /**
   * focusChanged
   * called by internal frames when a focus change occurs
  */
  public void someChange() {
    BNEditInternalFrame internalFrame =(BNEditInternalFrame) desktopPane.getSelectedFrame();
    if (internalFrame != null) {
        setSaveAsEnabled(true);
        setSaveEnabled(true);
        setMenuesEnabled(true);
        if (internalFrame.isChanged()) {
          setSaveAllEnabled(true);
        }
    }
    else {
        setSaveEnabled(false);
        setMenuesEnabled(false);
        setSaveAsEnabled(false);
        setSaveAllEnabled(false);
        JInternalFrame[] internalFrames = desktopPane.getAllFrames();
        for (int i = 0; i < internalFrames.length; i++) {
          BNEditInternalFrame frame = (BNEditInternalFrame) internalFrames[i];
          if (frame.isChanged()) {
             setSaveAllEnabled(true);
             return;
          }
        }
    }
  }

  /**
   * Sets all save as access points to a new enabled status
   * @param enabled boolean - the new state
  */
  private void setSaveEnabled(boolean enabled) {
    buttonSave.setEnabled(enabled);
    jMenuFileSave.setEnabled(enabled);
  }

  /**
   * Sets the option "Save as" to a new enabled status
   * @param enabled boolean - the new state
  */
  private void setSaveAsEnabled(boolean enabled) {
    jMenuSaveAs.setEnabled(enabled);
  }

  /**
   * Sets some menues to a new enabled status
   * @param enabled boolean - the new state
  */
  private void setMenuesEnabled(boolean enabled){
    jMenuNetwork.setEnabled(enabled);
    jMenuTables.setEnabled(enabled);
  }

  /**
   * Sets all save all access points to a new enabled status
   * @param enabled boolean
  */
  private void setSaveAllEnabled(boolean enabled) {
    buttonSaveAll.setEnabled(enabled);
    jMenuFileSaveAll.setEnabled(enabled);
  }

  /**
   * Gets the current directory for file Open/Save
   * @return String - path to the current working directory
  */
  public String getCurrDir() {
    return currDir;
  }

  /**
   * Sets the current working directory for file Open/Save
   * @param currDir String - path to the new working directory
  */
  public void setCurrDir(String currDir) {
    this.currDir = currDir;
  }

}

class BNEditMainFrame_jMenuFileExit_ActionAdapter implements ActionListener {
  BNEditMainFrame adaptee;

  BNEditMainFrame_jMenuFileExit_ActionAdapter(BNEditMainFrame adaptee) {
    this.adaptee = adaptee;
  }
  public void actionPerformed(ActionEvent e) {
    adaptee.exit();
  }
}

class BNEditMainFrame_jMenuHelpAbout_ActionAdapter implements ActionListener {
  BNEditMainFrame adaptee;

  BNEditMainFrame_jMenuHelpAbout_ActionAdapter(BNEditMainFrame adaptee) {
    this.adaptee = adaptee;
  }
  public void actionPerformed(ActionEvent e) {
    adaptee.helpAbout();
  }
}

class BNEditMainFrame_buttonNew_actionAdapter implements java.awt.event.ActionListener {
  BNEditMainFrame adaptee;

  BNEditMainFrame_buttonNew_actionAdapter(BNEditMainFrame adaptee) {
    this.adaptee = adaptee;
  }
  public void actionPerformed(ActionEvent e) {
    adaptee.newBayesianNetwork();
  }
}

class BNEditMainFrame_jMenuFileNew_actionAdapter implements java.awt.event.ActionListener {
  BNEditMainFrame adaptee;

  BNEditMainFrame_jMenuFileNew_actionAdapter(BNEditMainFrame adaptee) {
    this.adaptee = adaptee;
  }
  public void actionPerformed(ActionEvent e) {
    adaptee.newBayesianNetwork();
  }
}

class BNEditMainFrame_jMenuFileOpen_actionAdapter implements java.awt.event.ActionListener {
  BNEditMainFrame adaptee;

  BNEditMainFrame_jMenuFileOpen_actionAdapter(BNEditMainFrame adaptee) {
    this.adaptee = adaptee;
  }
  public void actionPerformed(ActionEvent e) {
    adaptee.openBayesianNetwork();
  }
}

class BNEditMainFrame_buttonOpen_actionAdapter implements java.awt.event.ActionListener {
  BNEditMainFrame adaptee;

  BNEditMainFrame_buttonOpen_actionAdapter(BNEditMainFrame adaptee) {
    this.adaptee = adaptee;
  }
  public void actionPerformed(ActionEvent e) {
    adaptee.openBayesianNetwork();
  }
}

class BNEditMainFrame_buttonSave_actionAdapter implements java.awt.event.ActionListener {
  BNEditMainFrame adaptee;

  BNEditMainFrame_buttonSave_actionAdapter(BNEditMainFrame adaptee) {
    this.adaptee = adaptee;
  }
  public void actionPerformed(ActionEvent e) {
    adaptee.saveCurrent();
  }
}

class BNEditMainFrame_jMenuFileSave_actionAdapter implements java.awt.event.ActionListener {
  BNEditMainFrame adaptee;

  BNEditMainFrame_jMenuFileSave_actionAdapter(BNEditMainFrame adaptee) {
    this.adaptee = adaptee;
  }
  public void actionPerformed(ActionEvent e) {
    adaptee.saveCurrent();
  }
}

class BNEditMainFrame_jMenuSaveAs_actionAdapter implements java.awt.event.ActionListener {
  BNEditMainFrame adaptee;

  BNEditMainFrame_jMenuSaveAs_actionAdapter(BNEditMainFrame adaptee) {
    this.adaptee = adaptee;
  }
  public void actionPerformed(ActionEvent e) {
    adaptee.saveCurrentAs();
  }
}

class BNEditMainFrame_buttonSaveAll_actionAdapter implements java.awt.event.ActionListener {
  BNEditMainFrame adaptee;

  BNEditMainFrame_buttonSaveAll_actionAdapter(BNEditMainFrame adaptee) {
    this.adaptee = adaptee;
  }
  public void actionPerformed(ActionEvent e) {
    adaptee.saveAll();
  }
}

class BNEditMainFrame_jMenuFileSaveAll_actionAdapter implements java.awt.event.ActionListener {
  BNEditMainFrame adaptee;

  BNEditMainFrame_jMenuFileSaveAll_actionAdapter(BNEditMainFrame adaptee) {
    this.adaptee = adaptee;
  }
  public void actionPerformed(ActionEvent e) {
    adaptee.saveAll();
  }
}

class BNEditMainFrame_buttonSL_actionAdapter implements java.awt.event.ActionListener {
  BNEditMainFrame adaptee;

  BNEditMainFrame_buttonSL_actionAdapter(BNEditMainFrame adaptee) {
    this.adaptee = adaptee;
  }
  public void actionPerformed(ActionEvent e) {
    adaptee.structuralLearning();
  }
}

class BNEditMainFrame_jBtMenuProbTable_actionAdapter implements java.awt.event.ActionListener {
  BNEditMainFrame adaptee;

  BNEditMainFrame_jBtMenuProbTable_actionAdapter(BNEditMainFrame adaptee) {
    this.adaptee = adaptee;
  }
  public void actionPerformed(ActionEvent e) {
    adaptee.potencialTable();
  }
}

class BNEditMainFrame_jBtMenuExpTable_actionAdapter implements java.awt.event.ActionListener {
  BNEditMainFrame adaptee;

  BNEditMainFrame_jBtMenuExpTable_actionAdapter(BNEditMainFrame adaptee) {
    this.adaptee = adaptee;
  }
  public void actionPerformed(ActionEvent e) {
    adaptee.experienceTable();
  }
}

class BNEditMainFrame_jMenuInitialize_actionAdapter implements java.awt.event.ActionListener {
  BNEditMainFrame adaptee;

  BNEditMainFrame_jMenuInitialize_actionAdapter(BNEditMainFrame adaptee) {
    this.adaptee = adaptee;
  }
  public void actionPerformed(ActionEvent e) {
      adaptee.initialize();
    }

}

class BNEditMainFrame_jMenuSumNormal_actionAdapter implements java.awt.event.ActionListener {
  BNEditMainFrame adaptee;

  BNEditMainFrame_jMenuSumNormal_actionAdapter(BNEditMainFrame adaptee) {
    this.adaptee = adaptee;
  }
  public void actionPerformed(ActionEvent e) {
    adaptee.sumNormal();
  }
}

class BNEditMainFrame_jMenuMaxNormal_actionAdapter implements java.awt.event.ActionListener {
  BNEditMainFrame adaptee;

  BNEditMainFrame_jMenuMaxNormal_actionAdapter(BNEditMainFrame adaptee) {
    this.adaptee = adaptee;
  }
  public void actionPerformed(ActionEvent e) {
    adaptee.maxNormal();
  }
}

class BNEditMainFrame_jMenuSumRetracted_actionAdapter implements java.awt.event.ActionListener {
  BNEditMainFrame adaptee;

  BNEditMainFrame_jMenuSumRetracted_actionAdapter(BNEditMainFrame adaptee) {
    this.adaptee = adaptee;
  }
  public void actionPerformed(ActionEvent e) {
    adaptee.sumRetracted();
  }
}

class BNEditMainFrame_jMenuMaxRetracted_actionAdapter implements java.awt.event.ActionListener {
  BNEditMainFrame adaptee;

  BNEditMainFrame_jMenuMaxRetracted_actionAdapter(BNEditMainFrame adaptee) {
    this.adaptee = adaptee;
  }
  public void actionPerformed(ActionEvent e) {
    adaptee.maxRetracted();
  }
}

class BNEditMainFrame_jMenuItemEdit_actionAdapter implements java.awt.event.ActionListener {
  BNEditMainFrame adaptee;

  BNEditMainFrame_jMenuItemEdit_actionAdapter(BNEditMainFrame adaptee) {
    this.adaptee = adaptee;
  }
  public void actionPerformed(ActionEvent e) {
    adaptee.edit();
  }
}

class BNEditMainFrame_jMenuRun_actionAdapter implements java.awt.event.ActionListener {
  BNEditMainFrame adaptee;

  BNEditMainFrame_jMenuRun_actionAdapter(BNEditMainFrame adaptee) {
    this.adaptee = adaptee;
  }
  public void actionPerformed(ActionEvent e) {
    adaptee.run();
  }
}

class BNEditMainFrame_jMenuNetProperties_actionAdapter implements java.awt.event.ActionListener {
  BNEditMainFrame adaptee;

  BNEditMainFrame_jMenuNetProperties_actionAdapter(BNEditMainFrame adaptee) {
    this.adaptee = adaptee;
  }
  public void actionPerformed(ActionEvent e) {
    adaptee.netProperties();
  }
}

class BNEditMainFrame_jMenuAdaptation_actionAdapter implements java.awt.event.ActionListener {
  BNEditMainFrame adaptee;

  BNEditMainFrame_jMenuAdaptation_actionAdapter(BNEditMainFrame adaptee) {
    this.adaptee = adaptee;
  }
  public void actionPerformed(ActionEvent e) {
    adaptee.adaptation();
  }
}

class BNEditMainFrame_jMenuAddExpTableAll_actionAdapter implements java.awt.event.ActionListener {
  BNEditMainFrame adaptee;

  BNEditMainFrame_jMenuAddExpTableAll_actionAdapter(BNEditMainFrame adaptee) {
    this.adaptee = adaptee;
  }
  public void actionPerformed(ActionEvent e) {
    adaptee.addExpTableAll();
  }
}

class BNEditMainFrame_jMenuFileClose_actionAdapter implements java.awt.event.ActionListener {
  BNEditMainFrame adaptee;

  BNEditMainFrame_jMenuFileClose_actionAdapter(BNEditMainFrame adaptee) {
    this.adaptee = adaptee;
  }
  public void actionPerformed(ActionEvent e) {
    adaptee.closeCurrentNet();
  }
}

class BNEditMainFrame_jMenuFileCloseAll_actionAdapter implements java.awt.event.ActionListener {
  BNEditMainFrame adaptee;

  BNEditMainFrame_jMenuFileCloseAll_actionAdapter(BNEditMainFrame adaptee) {
    this.adaptee = adaptee;
  }
  public void actionPerformed(ActionEvent e) {
    adaptee.closeAllNetworks();
  }
}



class BNEditMainFrame_jMenuNetwork_menuAdapter implements javax.swing.event.MenuListener {
  BNEditMainFrame adaptee;

  BNEditMainFrame_jMenuNetwork_menuAdapter(BNEditMainFrame adaptee) {
    this.adaptee = adaptee;
  }
  public void menuSelected(MenuEvent e) {
    adaptee.jMenuNetwork_menuSelected();
  }
  public void menuDeselected(MenuEvent e) {
  }
  public void menuCanceled(MenuEvent e) {
  }
}
